package com.neuedu;

import java.sql.*;
import java.math.BigDecimal;

public class HiveToMySQLB {
    public static void main(String[] args) {
        String hiveJdbcUrl = "jdbc:hive2://192.168.47.101:10000/vehicledb"; // Hive JDBC URL
        String mysqlJdbcUrl = "jdbc:mysql://192.168.47.101:3306/vehicledb"; // MySQL JDBC URL
        String mysqlUser = "root";
        String mysqlPassword = "123456";

        Connection hiveConn = null;
        Connection mysqlConn = null;
        Statement hiveStmt = null;
        Statement mysqlStmt = null;
        ResultSet rs = null;

        try {
            // 1. 连接到 Hive
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            hiveConn = DriverManager.getConnection(hiveJdbcUrl, "", "");
            hiveStmt = hiveConn.createStatement();

            // 2. 从 Hive 查询数据
            String hiveQuery = "SELECT region, category, parameter, mode, powertrain, year, unit, value FROM b_global_ev";
            rs = hiveStmt.executeQuery(hiveQuery);

            // 3. 连接到 MySQL
            Class.forName("com.mysql.cj.jdbc.Driver");
            mysqlConn = DriverManager.getConnection(mysqlJdbcUrl, mysqlUser, mysqlPassword);
            mysqlStmt = mysqlConn.createStatement();

            // 4. 创建 MySQL 插入语句
            String insertQuery = "INSERT INTO b_global_ev (region, category, parameter, mode, powertrain, year, unit, value) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement mysqlStmtPrepared = mysqlConn.prepareStatement(insertQuery);

            // 5. 将数据从 Hive 导入 MySQL
            while (rs.next()) {
                String region = rs.getString("region");
                String category = rs.getString("category");
                String parameter = rs.getString("parameter");
                String mode = rs.getString("mode");
                String powertrain = rs.getString("powertrain");
                int year = rs.getInt("year");
                String unit = rs.getString("unit");
                BigDecimal value = rs.getBigDecimal("value");

                // 设置参数
                mysqlStmtPrepared.setString(1, region);
                mysqlStmtPrepared.setString(2, category);
                mysqlStmtPrepared.setString(3, parameter);
                mysqlStmtPrepared.setString(4, mode);
                mysqlStmtPrepared.setString(5, powertrain);
                mysqlStmtPrepared.setInt(6, year);
                mysqlStmtPrepared.setString(7, unit);
                mysqlStmtPrepared.setBigDecimal(8, value);

                // 执行插入
                mysqlStmtPrepared.executeUpdate();
            }

            System.out.println("Data successfully transferred from Hive to MySQL (b_global_ev)");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (hiveStmt != null) hiveStmt.close();
                if (hiveConn != null) hiveConn.close();
                if (mysqlStmt != null) mysqlStmt.close();
                if (mysqlConn != null) mysqlConn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

